﻿using System;
using System.Collections.Generic;
using System.Xml;
using OpenGLES;

namespace PFD
{
    public class MultilayerTexture : IRenderTextureProvider
    {
        public List<MultilayerTextureLayer> layerList;

        public MultilayerTexture()
        {
            layerList = new List<MultilayerTextureLayer>();
        }

        public bool LoadFromXML(XmlNode layers, int nDimension,
            GlobalPoolContainer pool, ref IVertexBuffer ivb)
        {
            if (layers.ChildNodes.Count < 1)
                throw new ArgumentOutOfRangeException("Layers", "Invalid number of texture layers detected");

            uint nLayer = 0;
            foreach (XmlNode layer in layers.ChildNodes)
            {
                MultilayerTextureLayer l = new MultilayerTextureLayer(nLayer);
                layerList.Add(l);
                l.LoadFromXML(layer, pool, ref ivb);
            }

            return true;
        }

        #region IRenderTextureProvider Members

        public IRenderTexture this[uint index]
        {
            get 
            { 
                if (layerList.Count<=index)
                    throw new ArgumentOutOfRangeException("index", "Invalid index in MultilayerTexture");
                return layerList[(int)index].Texture;
            }
        }

        public bool this[uint index, RenderTextureProviderState state]
        {
            get 
            {
                if ((state != RenderTextureProviderState.BlendingEnabled) ||
                    (layerList.Count<=index))
                    throw new ArgumentOutOfRangeException("state", "Invalid state requested for RenderTesture in MultilayerTexture");
                return layerList[(int)index].EnableBlending;

            }
        }

        public uint Count
        {
            get 
            {
                return (uint)layerList.Count;
            }
        }

        #endregion
    }
}
